from dataclasses import asdict

from framework.http import Request
from model.contacts_api import ContactsAApp
from model.member import Member

class ContactsApi:
    _token = ''

    def refresh_token(self, contract_app: ContactsApp):
        request = Request()
        request.path = "/cgi-bin/gettoken"
        request.method = "get"
        request.param = {
            "corpid": contract_app.corpid,
            "corpsecret": contract_app.secrret
        }
        r = request.send()
        self._token = r.json().get("access_token")
        print(r.text)

    def get_token(self):
        return self._token

    def add_member(self, member: Member):
        request = Request()
        request.path = "/cgi-bin/user/create"
        request.method = "post"
        request.param = {
            "access_token": self._token,
            "userid": user_id
        }
        r = request.send()
        print(r.json())

    def delete_member(self, user_id):
        request = Request()
        request.path = "/cgi-bin/user/delete"
        request.method = {
            "access_token": self._token,
            "userid": user_id
        }
        request.send()